package cn.wyj.controller;

import cn.wyj.dao.CommentMapper;
import cn.wyj.domain.Article;
import cn.wyj.domain.Comment;
import cn.wyj.service.ArticleService;
import cn.wyj.service.CommentService;
import cn.wyj.service.SiteService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class ArticleController {
    @Autowired
    private ArticleService articleService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private SiteService siteService;

    @GetMapping("/")
    private String index(){
        return "redirect:/login";
    }
    @GetMapping("/page/{page}")
    private String index(Model model, @PathVariable("page") Integer page, @RequestParam(value = "count",defaultValue = "5") Integer count){
        PageInfo<Article> articles = articleService.findArticlesByPage(page,count);
        //获取文章热度信息
        List<Article> articleList = articleService.getHeatArticles();
        model.addAttribute("articles",articles);
        model.addAttribute("articleList",articleList);
        return "client/index";
    }

    @GetMapping("/article/{id}")
    private String findById(Model model,@PathVariable("id") Integer id){
        Article article = articleService.findById(id);
        PageInfo<Comment> comments = commentService.getComments(id, 1, 3);
        if(article!=null){
            siteService.updateStatistic(article);
            model.addAttribute("article",article);
            model.addAttribute("comments",comments);
        }else{
            return "comm/error_404";
        }

        return "client/articleDetails";
    }
}
